﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace LoginTest
{
    public partial class LoginFrm : Form
    {
        public static string selectFacotoryname = "";
        public static string userID = "";
        public static string password = "";
        public static string roleName = "";
        public static string department = "";
        public LoginFrm()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox2.Items.Add("superadmin");
            comboBox2.Items.Add("cc");
            comboBox2.Items.Add("yy");
            comboBox2.Items.Add("zz");
            comboBox2.Items.Add("kk");
            comboBox2.Items.Add("man");
            comboBox2.Items.Add("admin");
            comboBox2.Items.Add("dd");
            comboBox3.Items.Add("超级管理员");
            comboBox3.Items.Add("仓管员");
            comboBox3.Items.Add("运输员");
            comboBox3.Items.Add("质检员");
            comboBox3.Items.Add("客户跟单员");
            comboBox3.Items.Add("普通管理员");
            // 建立数据库连接并查询工厂名
            using (SqlConnection connection = new SqlConnection(@"Data Source=.;Initial Catalog = GXFactoryDB;Integrated Security = True"))
            {
                string query = "select FactoryName from Factory_tb ";
                SqlCommand command = new SqlCommand(query, connection);

                connection.Open();

                SqlDataReader reader = command.ExecuteReader();

                while (reader.Read())
                {
                    comboBox1.Items.Add(reader["FactoryName"].ToString());
                }

                connection.Close();
            }


        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }


        private void Login_Click(object sender, EventArgs e)
        {
            if (this.comboBox1.Text == "")
            {
                MessageBox.Show("请选择工厂！", "登陆提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                return;
            }
            if (this.comboBox2.Text == "")
            {
                MessageBox.Show("请选择用户名！", "登陆提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                return;
            }
            if (this.comboBox3.Text == "")
            {
                MessageBox.Show("请选择身份！", "登陆提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                return;
            }
            selectFacotoryname = comboBox1.SelectedItem.ToString();
            userID = comboBox2.SelectedItem.ToString();
            roleName = comboBox3.SelectedItem.ToString();
            password = textBox1.Text.ToString();
            if (selectFacotoryname != "" && userID != "" && roleName != "" && password != "")
            {
                if (ValidateCredentials(userID, roleName, password))
                {
                    if (selectFacotoryname == "云和森友")
                    {
                        
                        new MainForm().Show();
                    department = GetDepartmentFromDatabase(userID);
                    label6.Text = department;
                        this.Hide();
                    }
                   
                    
                }
                else
                {
                    MessageBox.Show("登陆失败!");
                }
            }

        }
        //验证是否可以登录
        private bool ValidateCredentials(string userID, string roleName, string password)
        {
            using (SqlConnection connection = new SqlConnection(@"Data Source=.;Initial Catalog = GXFactoryDB;Integrated Security = True"))
            {
                string query = "SELECT * FROM User_tb where UserName=@UserName and UserRole=@UserRole and UserPwd=@UserPwd";
                SqlCommand command = new SqlCommand(query, connection);
                //command.Parameters.AddWithValue("@FactoryName", selectFacotoryname);
                command.Parameters.AddWithValue("@UserName", userID);
                command.Parameters.AddWithValue("@UserRole", roleName);
                command.Parameters.AddWithValue("@UserPwd", password);

                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                // 如果查询到匹配的结果，返回true；否则，返回false
                if (reader.Read()){
                    MessageBox.Show("登录成功," + roleName+ "你好");
                    connection.Close();
                    return true;
                }
                else{
                    MessageBox.Show("登录失败");
                    connection.Close();
                    return false;
                }
            }
        }

        public static string GetDepartmentFromDatabase(string userID)
        {
            using (SqlConnection connection = new SqlConnection(@"Data Source=.;Initial Catalog = GXFactoryDB;Integrated Security = True"))
            {
                string query = "select FactoryDepartment from User_tb WHERE UserName = @UserName";
                SqlCommand command = new SqlCommand(query, connection);
                command.Parameters.AddWithValue("@UserName", userID);

                connection.Open();
                SqlDataReader reader = command.ExecuteReader();
                if (reader.Read())
                {
                    // 获取部门列的值
                    department = reader["FactoryDepartment"].ToString();
                    return department;
                }
                else{
                    return "Unknown";
                }
            }
        }

        private void Close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                textBox1.Text = "123456";
            }
        }
    }
}
